package com.ydlclass.consumer.controller;

import com.ydlclass.consumer.domain.Goods;
import com.ydlclass.consumer.fegin.GoodsFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

//    @Autowired
//    private RestTemplate restTemplate;
//
//    @Autowired
//    private DiscoveryClient discoveryClient;

    @Autowired
    private GoodsFeign goodsFeign;

    @GetMapping("/add/{id}")
    public Goods add(@PathVariable("id") Integer id){

//        List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
//        if (instances==null||instances.size()<=0){
//            return null;
//        }
//        ServiceInstance serviceInstance = instances.get(0);
//        int port=serviceInstance.getPort();
//        String host=serviceInstance.getHost();


//        String url="http://eureka-provider/goods/findById/"+id;
//        Goods goods = restTemplate.getForObject(url, Goods.class);
        Goods goods = goodsFeign.findById(id);
        if (goods.getGoodId()==-1){

            /**
             * 打日志
             * ....
             */
        }
        return goods;
    }
}
